//index.js
//获取应用实例

var dragArea = require('./dragArea.js');
let cax = require("./cax/index");
var app = getApp();

let gStage = null;
let gIsIphone = false;
let testAniIndex = 0;

import testData from "../utils/testPersonData.js"
import testMenuData from "../utils/testMenuData.js"

Page({
        data: {
                canvasWidth: 0,
                canvasHeight: 0,
                pixelRatio: 1,
                bgAnimation: null,

                bgUrl: app.globalData.baseUrl + '/gif/bg1.gif',

                bottomBtnData: testData.changeBtns,
                rightMenuData: testMenuData.rightMenuItemData,
                rightMenuId: 0,
        },

        onReady: function () {
                console.log('onLoad');
                var _self = this;
                let info = jd.getSystemInfoSync();
                gIsIphone = info.platform == "ios";
                console.log(info)
                this.setData({
                        canvasHeight: info.windowHeight ? info.windowHeight * 0.65 : 180 * 1.5,
                        canvasWidth: info.windowWidth ? info.windowWidth * 0.7 : 180,
                        pixelRatio: info.pixelRatio ? info.pixelRatio : 2
                });

                dragArea.initStage(this, this.data.canvasWidth, this.data.canvasHeight, 'caxcanvas', this.data.pixelRatio, false);
                this.animation = jd.createAnimation()

                this.bottomMenuView = this.selectComponent("#bottomview"); //组件的id
        },

        bindViewTap(inTag) {
                let tempTag = inTag;
                let tempTransY = 0;
                let tempScale = 1;
                if (inTag == "top") {
                        tempTransY = 80;
                        tempScale = 1.3;
                } else if (inTag == "center") {
                        tempTransY = 0;
                        tempScale = 1;
                } else if (tempTag = "bottom") {
                        tempTransY = -80;
                        tempScale = 1.3;
                }

                console.log("bindViewTap --- " + tempTag)
                dragArea.moveBodyAni(tempTag);

                // bg Ani
                this.animation.translate(0, tempTransY).scale(tempScale).step({
                        duration: 200,
                        timingFunction: "ease"
                })
                this.setData({
                        bgAnimation: this.animation.export()
                })
                // bg Ani

                // dragArea.startSpirit()
        },

        bottomBtnClick(e) {
                console.log(e);
                let tempBtnId = e.currentTarget.dataset.data.btnId;
                let dataList = this.data.bottomBtnData;
                let tempBtnData = null;

                tempBtnData = dataList.find(e => {
                        return e.btnId == tempBtnId;
                })

                if (!tempBtnData || !tempBtnData.changeParts)
                        return;
                this.bindViewTap(tempBtnData.aniTag)
                let tempSelectIndex = tempBtnData.selectIndex;
                tempSelectIndex++;
                if (tempSelectIndex >= tempBtnData.changeParts.length)
                        tempSelectIndex = 0;

                tempBtnData.selectIndex = tempSelectIndex;

                this.setData({
                        bottomBtnData: dataList
                })

                console.log(this.data.bottomBtnData)
                let tempChangeData = tempBtnData.changeParts.find((e) => {
                        return e.partyIndex == tempSelectIndex
                })

                if (tempChangeData) {
                        debugger
                        if(tempBtnData.btnId == 8) {                        
                                // this.freeDressView.changeBgByData(tempChangeData);
                                this.setData({
                                        bgUrl: app.globalData.baseUrl + tempChangeData.partyList[0].partImage,
                                })
                                return;
                        }


                        for (let i = 0; i < tempChangeData.partyList.length; i++) {
                                let tempData = tempChangeData.partyList[i];
                                dragArea.changePartByExternalData(tempData);
                        }
                }

                // aniTag

        },

        /**
         * 右侧菜单按钮选择
         * @param {*} e 
         */
        onRightMenuItemChange(e) {
                console.log("onRightMenuItemChange" + e.detail.selectItemId)
                this.setData({
                        rightMenuId: e.detail.selectItemId
                })

                let rightData = this.data.rightMenuData[this.data.rightMenuId];
                // this.bottomMenuView.setSelectRightMenuId(e.detail.selectItemId);
        },

        touchStart(e) {
                console.log("touchStart --- ");
                // console.log(`${e.touches[0].x} --- ${e.touches[0].y}`);
                gStage.touchStartHandler(e);
        },

        touchMove(e) {
                console.log("touchMove --- ");
                // console.log(`${e.touches[0].x} --- ${e.touches[0].y}`);
                gStage.touchMoveHandler(e);

        },

        touchEnd(e) {
                console.log("touchEnd --- ");
                gStage.touchEndHandler(e);
        },



        touchTargetStart(e) {
                console.log("tttttt---touchStart --- ");
                // console.log(`${e.touches[0].x} --- ${e.touches[0].y}`);

        },

        touchTargetMove(e) {
                console.log("tttttt---touchMove --- ");
                // console.log(`${e.touches[0].x} --- ${e.touches[0].y}`);

        },

        touchTargetEnd(e) {
                console.log("tttttt---touchEnd --- ");
        },
});